package at.fhooe.SRWCProject.Bluetooth.Socket;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.os.Handler;
import at.fhooe.SRWCProject.Bluetooth.ChatActivity;
import at.fhooe.SRWCProject.Bluetooth.Socket.ConnectionThread;
import at.fhooe.SRWCProject.MainActivity;

import java.io.IOException;
import java.net.Socket;
import java.util.UUID;

public class AcceptThread extends Thread{
    private final Handler mmHandler;
    private BluetoothServerSocket mmServerSocket = null;
    private BluetoothSocket mmSocket = null;

    public AcceptThread(BluetoothAdapter mBluetoothAdapter, Handler handler,UUID uuid){
        mmHandler = handler;
        try{
            mmServerSocket = mBluetoothAdapter.listenUsingRfcommWithServiceRecord("BluetoothDemo",uuid);
        }catch(IOException e){
            e.printStackTrace();
        }
    }

    public void run(){
        while(true){
            try{
                mmSocket = mmServerSocket.accept();
                ConnectionThread conn = new ConnectionThread(mmSocket, mmHandler);
                mmHandler.obtainMessage(MainActivity.SOCKET_CONNECTED, conn).sendToTarget();
                conn.start();
                mmServerSocket.close();
                break;
            }catch (Exception e){
                e.printStackTrace();
            }

        }
    }
}